package com.example.RemindMe;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.example.jBCrypt.BCrypt;


public class RegisterActivity extends Activity {


    private EditText password;
    private EditText passwordRetype;
    private EditText regCode;
    private Button b;
    Context ctx;
    private static String sharedPrefName2="sharedPrefName2";


    //TextWatcher
    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3)
       {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            checkFieldsForEmptyValues();
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_activity);
        ctx=this;


        //load SharedPref
        SharedPreferences sharedPreferences = getSharedPreferences(sharedPrefName2, MODE_PRIVATE);
        Boolean sharedBoolean2 = sharedPreferences.getBoolean("sharedBoolean2", false);

        //Show Overlay(visual hints) if sharedBoolean2 == false
        if (sharedBoolean2 == false){

            //save  SharedPref
            sharedPreferences = getSharedPreferences(sharedPrefName2, MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean("sharedBoolean2", true);
            editor.commit();
            showOverLay();
        }
        
        
        password = (EditText) findViewById(R.id.reg_password);
        passwordRetype = (EditText) findViewById(R.id.reg_password2);
        regCode = (EditText) findViewById(R.id.reg_code);
        b = (Button) findViewById(R.id.btnRegister);

        //set listeners
        password.addTextChangedListener(textWatcher);
        passwordRetype.addTextChangedListener(textWatcher);
        regCode.addTextChangedListener(textWatcher);

        // run once to disable if empty
        checkFieldsForEmptyValues();
    }

    private  void checkFieldsForEmptyValues(){
        String s1 = password.getText().toString();
        String s2 = passwordRetype.getText().toString();
        String s3 = regCode.getText().toString();

        if (s1.length() > 0 && s2.length() > 0 && s3.length()>0 ) {
            b.setEnabled(true);
        } else {
            b.setEnabled(false);
        }
    }

    public void setNewPassword(View view)
    {
    	
    	 //delete database
    	 NotesDbAdapter nba = new NotesDbAdapter(this);
         nba.deleteDB();

        String s1 = password.getText().toString();
        String s2 = passwordRetype.getText().toString();
        String s3 = regCode.getText().toString();

        if(! s1.equals(s2))
        {
            Toast.makeText(this,"These passwords don't match. Try again?",Toast.LENGTH_LONG).show();
        }
        else
        {
        	SharedPreferences sharedPreferences2 = getSharedPreferences("registrationScreen", MODE_PRIVATE);
        	 //SharedPref verwenden um überprüfen zu könen, dass Registration-Screen nur beim ersten Mal angzeigt wird
            sharedPreferences2 = getSharedPreferences("registrationScreen", MODE_PRIVATE);
            SharedPreferences.Editor editor2 = sharedPreferences2.edit();
            editor2.putBoolean("registrationSetting", false);
            editor2.commit();

        	
            SharedPreferences sharedPreferences = getSharedPreferences("passwordEncrypted", MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("pw", BCrypt.hashpw(s1,BCrypt.gensalt()));
            editor.commit();

            sharedPreferences = getSharedPreferences("codeEncrypted", MODE_PRIVATE);
            SharedPreferences.Editor editor1 = sharedPreferences.edit();
            editor1.putString("code", BCrypt.hashpw(s3,BCrypt.gensalt()));
            editor1.commit();

            startActivity (new Intent(getApplicationContext(), DialogActivity.class));
            finish();
            Toast.makeText(this,"Password and Code created!",Toast.LENGTH_LONG).show();

        }

    }
    
    public void showOverLay(){

        final Dialog dialog = new Dialog(ctx, android.R.style.Theme_Translucent_NoTitleBar);
        dialog.setContentView(R.layout.overlay_view_2);
        RelativeLayout layout = (RelativeLayout) dialog.findViewById(R.id.overlayLayout_2);
        layout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }
}
